﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace SuperStar.Models
{
     [MetadataType(typeof(ProductPrmotionMetaData))]
    public partial class ProductPromotion
     {
        protected class ProductPrmotionMetaData
        { 
            [Required(ErrorMessage = "Tên sản phẩm khuyến mãi không hợp lệ")]
            public String Name { get; set; }
            [Required(ErrorMessage = "Điểm yêu cầu không hợp lệ")]
            public String GiftPointRequired { get; set; }
            [Required(ErrorMessage = "Số lượng nhận khuyến mãi tối đa không hợp lệ")]
            public String MaxGiftAmount { get; set; }
            [Required(ErrorMessage = "Số lượng không hợp lệ")]
            public String Quantity { get; set; }
            public String GiftOfferedCount { get; set; }
        }
     }
    public class ProductPromotionModels
    {
        public SuperStarEntities DB { get; set; }
        public String DEBUGTYPE { get; set; }

        public ProductPromotionModels()
        {
               DB = new SuperStarEntities();
        }
        public int GetCountAllProducts()
        {
            return DB.Products.Count();
        }
       public List<ProductPromotion> GetAvaidProductPromotions(int billId)
       {
           return DB.spBill_GetAvaildProductPromotions(billId).ToList();
       }
        public List<ProductPromotion> GetProductPromotions(int productId)
        {
            var pm = from p in DB.ProductPromotions
                     where p.ProductId == productId
                     select p;
            return pm.ToList();
        }
        public List<ProductPromotion> GetProductPromotion(int id)
        {
            var pm = from p in DB.ProductPromotions
                     where p.Id == id
                     select p;
            return pm.ToList();
        }
        public bool Update(ProductPromotion p)
        {
            if(DEBUGTYPE.Equals("7_error"))
            {
                return DB.spTH07_ProductPromotion_error_UpdateProductPromotion
                        (p.Name, p.Id, p.GiftPointRequired, p.Quantity, p.MaxGiftAmount) > 0;
            }
            if (DEBUGTYPE.Equals("7_fix"))
            {
                return DB.spTH07_ProductPromotion_fix_UpdateProductPromotion
                        (p.Name, p.Id, p.GiftPointRequired, p.Quantity, p.MaxGiftAmount) > 0;
            }
            return DB.spUpdateProductPromotion
                        (p.Name, p.Id, p.GiftPointRequired,p.Quantity, p.MaxGiftAmount) > 0;
        }
        public ProductPromotion Insert(ProductPromotion p)
        {
            return DB.spProductPromotion_Insert(p.Name, p.GiftPointRequired,
               p.MaxGiftAmount, p.RemainAmount, p.Quantity, p.GiftOfferedCount,p.ProductId).First();
        }
        public bool Delete(int id)
        {
            return DB.spProductPromotion_Delete(id) > 0;
        }
    }
}
